/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuDevice;
import com.boxin.ims.modules.jiujiudevice.service.JiuDeviceService;

/**
 * 设备配置Controller
 * @author sxh
 * @version 2018-08-15
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuDevice")
public class JiuDeviceController extends BaseController {

	@Autowired
	private JiuDeviceService jiuDeviceService;
	
	@ModelAttribute
	public JiuDevice get(@RequestParam(required=false) Long id) {
		if (id != null){
			return jiuDeviceService.get(id);
		}else{
			return new JiuDevice();
		}
	}
	
	@RequiresPermissions("jiujiudevice:jiuDevice:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuDevice jiuDevice, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		/*if (!user.isAdmin()){
			jiuDevice.setUser(user);
		}*/
        Page<JiuDevice> page = jiuDeviceService.find(new Page<JiuDevice>(request, response), jiuDevice); 
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuDeviceList";
	}

	@RequiresPermissions("jiujiudevice:jiuDevice:view")
	@RequestMapping(value = "form")
	public String form(JiuDevice jiuDevice, Model model) {
	
		User user = UserUtils.getUser();
		/*if (jiuDevice.getUser() == null || jiuDevice.getUser().getId() <1){
			jiuDevice.setUser(user);
		}
		*/
		model.addAttribute("jiuDevice", jiuDevice);
		return "modules/jiujiudevice/jiuDeviceForm";
	}

	@RequiresPermissions("jiujiudevice:jiuDevice:edit")
	@RequestMapping(value = "save")
	public String save(JiuDevice jiuDevice, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuDevice)){
			return form(jiuDevice, model);
		}
		jiuDeviceService.save(jiuDevice);
		addMessage(redirectAttributes, "保存设备配置'" + jiuDevice.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuDevice/?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuDevice:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuDeviceService.delete(id);
		addMessage(redirectAttributes, "删除设备配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuDevice/?repage";
	}

}
